package com.common.www.controller.tabulation;


import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.project.SupplyCompany;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.project.SupplyCompanyService;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/supplyCompany"})
@Api(tags = "MGT 后台供货单位管理")
public class MGTSupplyCompanyController extends DefaultCrudController<SupplyCompany,Long> {

    @Autowired
    private SupplyCompanyService supplyCompanyService;

    protected IBaseService<SupplyCompany,Long> getService() {
        return supplyCompanyService;
    }


    @ApiOperation("保存供货单位")
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(@RequestBody SupplyCompany supplyCompany) {
        Map<String,Object> param =new HashMap<>();
        param.put("name",supplyCompany.getName());
        if(supplyCompany.getId()!=null){
            param.put("id_ne",supplyCompany.getId());
        }
        List<SupplyCompany> supplyCompanyList =  supplyCompanyService.getListLogic(param,null);
        if(supplyCompanyList.size()>0){
            return R.fail("供应商名称重复");
        }
        supplyCompanyService.save(supplyCompany);
        return R.ok(supplyCompany);
    }

    @ApiOperation("批量删除供货单位（逻辑删除）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids) {
        if (ParamsUtils.isInputEmpty(ids)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        supplyCompanyService.deleteByIdsLogic(ids);
        return R.ok();
    }

}
